package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.User;
import com.example.demo.common.BaseResult;
import com.example.demo.config.token.TokenUtils;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class LoginController {

	/**
	 * logout 注销
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/logout", method = RequestMethod.GET)
	public BaseResult logoutPage(HttpServletRequest request, HttpServletResponse response) {
		// 获取令牌
		User token;
		try {
			token = TokenUtils.getToken();
			if (token != null) {
				TokenUtils.logout(request, response);
				return new BaseResult(200, "登出成功", null);
			} else {
				return new BaseResult(500, "注销失败", null);
			}
		} catch (Exception e) {
			log.info("注销报告：一般与无法找到用户 ：" + e.getMessage());
			return new BaseResult(401, "注销失败，未登录", null);
		}
	}
}
